package com.example.xxljobdemo;

public class WaitNotifyExample {

    private static final Object lock = new Object();

    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> {
            synchronized (lock) {
                try {
                    System.out.println("Thread 1 is waiting...");
                    lock.wait();
                    System.out.println("Thread 1 is notified.");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread thread2 = new Thread(() -> {
            synchronized (lock) {
                try {
                    System.out.println("Thread 2 is waiting...");
                    lock.wait();
                    System.out.println("Thread 2 is notified.");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread notifyThread = new Thread(() -> {
            synchronized (lock) {
                // 唤醒一个等待的线程
                lock.notify();

                // 唤醒所有等待的线程
//                lock.notifyAll();
            }
        });

        thread1.start();
        thread2.start();

        try {
            Thread.sleep(1000); // 等待一段时间，确保线程1和线程2都进入等待状态
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        notifyThread.start();
    }

}
